# $Id$
# Maintainer: Evangelos Foutras <evangelos@foutrelis.com>

pkgname=('geoip-database' 'geoip-database-extra')
pkgver=20171003
pkgrel=1
arch=('any')
url="https://dev.maxmind.com/geoip/legacy/geolite/"
license=('custom:OPEN DATA LICENSE')
checkdepends=('geoip')
source=(GeoIP-$pkgver.dat.gz::https://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
        GeoIPv6-$pkgver.dat.gz::https://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
        GeoLiteCity-$pkgver.dat.gz::https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
        GeoLiteCityv6-$pkgver.dat.gz::https://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz
        GeoIPASNum-$pkgver.dat.gz::https://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
        GeoIPASNumv6-$pkgver.dat.gz::https://download.maxmind.com/download/geoip/database/asnum/GeoIPASNumv6.dat.gz
        GeoIP-LICENSE.txt::https://geolite.maxmind.com/download/geoip/database/LICENSE.txt)
noextract=(GeoIP-$pkgver.dat.gz
           GeoIPv6-$pkgver.dat.gz
           GeoLiteCity-$pkgver.dat.gz
           GeoLiteCityv6-$pkgver.dat.gz
           GeoIPASNum-$pkgver.dat.gz
           GeoIPASNumv6-$pkgver.dat.gz)
sha256sums=('54d33334101a6e69f2877820d8d46a6c956f72716aa76c41c6f9a1d9bba51df7'
            'f36aff9ae567990ff36662abde3a44497e9ba79d34fd0d43c9a3c22a8ba86a3c'
            '19079e8d7b757e3cda77ca14978f02662f110e7c02ab797dfaf856d15f0adceb'
            '7337386f63bf28d914171bd6ea46b1229a05c6acf08d13915938ade242c50b46'
            '3cff8f6f08b6adf5a4ef4c0f7dc4b24093e43f7cd89f3ce2eeef921994f4ba27'
            '1f0de4b7296f217af58e61b757b5a08bcf4e6e40ef30124573ca97df6285e745'
            '83a4cb82ef8953c0107886b49bc39fcf77590a222d30e778d48f5d92f5b5e383')

prepare() {
  cd "$srcdir"

  for _database_name in GeoIP GeoLiteCity GeoIPASNum; do
    gunzip -c $_database_name-$pkgver.dat.gz >${_database_name/GeoLite/GeoIP}.dat
    gunzip -c ${_database_name}v6-$pkgver.dat.gz >${_database_name/GeoLite/GeoIP}v6.dat
  done
}

check() {
  cd "$srcdir"

  if [[ $(geoiplookup -f GeoIP.dat 8.8.8.8) != *'US, United States' ]]; then
    error 'Unable to resolve IPv4 address to country.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPv6.dat 2001:4860:4860::8888) != *'US, United States' ]]; then
    error 'Unable to resolve IPv6 address to country.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPCity.dat 8.8.8.8) != *'US, N/A, N/A'* ]]; then
    error 'Unable to resolve IPv4 address to city.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPCityv6.dat 2001:4860:4860::8888) != *'US, N/A, N/A'* ]]; then
    error 'Unable to resolve IPv6 address to city.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPASNum.dat 8.8.8.8) != *'AS15169 Google Inc.' ]]; then
    error 'Unable to resolve IPv4 address to ASN.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPASNumv6.dat 2001:4860:4860::8888) != *'AS15169 Google Inc.' ]]; then
    error 'Unable to resolve IPv6 address to ASN.'
    return 1
  fi
}

package_geoip-database() {
  pkgdesc="GeoLite country geolocation database compiled by MaxMind"

  cd "$srcdir"

  install -d "$pkgdir/usr/share/GeoIP"
  install -m644 -t "$pkgdir/usr/share/GeoIP" GeoIP{,v6}.dat

  install -Dm644 GeoIP-LICENSE.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE.txt"
}

package_geoip-database-extra() {
  pkgdesc="GeoLite city/ASN geolocation databases compiled by MaxMind"

  cd "$srcdir"

  install -d "$pkgdir/usr/share/GeoIP"
  install -m644 -t "$pkgdir/usr/share/GeoIP" GeoIPCity{,v6}.dat GeoIPASNum{,v6}.dat

  install -Dm644 GeoIP-LICENSE.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE.txt"
}

# vim:set ts=2 sw=2 et:
